public class CuratorPS {
	
	//name of Curator process
	private String name="";
	//PID ,0 for not open
	private int pid=0;
	//location of startScript, read from config file
	private String startScript="";
	
	//if it is on
	private boolean on=false;
	
	//if it is an java tool. (we will decide to use jps or ps |grep name)
	private boolean java=false;

	/**
	 * @param line
	 */
	public CuratorPS(String line) {
		String[] tmp = line.split(" ");
		if (tmp.length == 2) {
			this.pid = Integer.parseInt(tmp[0]);
			this.name = tmp[1];
		}
		if (tmp.length == 0) {
			this.pid = 0;
			this.name = "null";
		}
		if (tmp.length == 1) {
			this.pid = Integer.parseInt(tmp[0]);
			this.name = "null";
		}

	}

	/**
	 * @param line
	 * @param java
	 */
	public CuratorPS(String line, boolean java) {
		String[] tmp = line.split(",");
		this.java=java;
		if (tmp.length == 2) {
			this.name = (tmp[0]);
			this.startScript = tmp[1];
		}
		if (tmp.length == 0) {

			this.name = "null";
		}
		if (tmp.length == 1) {
			// this.pid=Integer.parseInt(tmp[0]);
			// this.name="null";
		}
		this.pid=Util.getPID(this.name, this.java);
		this.on=this.pid!=0;
		
		
	}

	/**
	 * @param name
	 * @param pid
	 */
	public CuratorPS(String name, int pid) {
		this.name = name;
		this.pid = pid;
		// this.type=findType(this.jpsName);
		// this.name=findName(this.jpsName);
	}

	/**
	 *   kill this curator tool.
	 */
	public void kill() {
		Util.exec("kill -9" + this.pid);
	}

	/**
	 *   update PID
	 */
	public void updatePID(){
		this.pid=Util.getPID(this.name, this.java);
	}
	
	/**
	 *  Start the curator tool
	 */
	public void start() {
		Util.exec(this.startScript);
	}
	/**
	 * @return Name
	 */
	public String getName(){
		//TextIO.putln(this.name+":"+this.java);
		return this.name;
	}
	
	
	/**
	 * @return the html-string.
	 */
	public String toHTML() {
		if(this.pid==0)this.on=false;
		else
			this.on=true;
		String ret = "";

		String button = "<button name=\"Mycommand\" type=\"submit\" value=\""+ "open:" + this.name+"\">"+ "open " + this.name+"</button>";

		if(on)button="<button name=\"Mycommand\" type=\"submit\" value=\""+ "close:" + this.pid+"\">"+ "close " + this.pid+"</button>";
		ret = "<tr><td>" + this.pid + "</td><td>" + this.name + "</td><td>"
				+ button + "</td></tr>";
		return ret;
	}
	
	
	
}
